package com.cec.service.admin.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cec.dto.CategoryDTO;
import com.cec.exception.DataLoadingException;
import com.cec.exception.OperationDataException;
import com.cec.mapper.admin.InfoMapper;
import com.cec.result.ResultCode;
import com.cec.service.admin.InfoService;
import com.cec.dto.InfoDTO;
import com.cec.entity.Info;
import com.cec.result.Result;
import com.cec.utils.HttpClientUtil;
import com.cec.utils.JsoupUtil;
import com.cec.vo.HomeDataVO;
import com.cec.vo.InfoVO;
import com.cec.vo.CategoryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 资讯表 服务实现类
 * </p>
 *
 * @author lt
 * @since 2024-10-25
 */
@Slf4j
@Service
public class InfoServiceImpl extends ServiceImpl<InfoMapper, Info> implements InfoService {

    @Override
    public Result pageQueryInfo(InfoDTO infoDTO) {
        QueryWrapper<InfoDTO> wrapper = new QueryWrapper<>();
        if (infoDTO.getInfoTitle()!=null && !"".equals(infoDTO.getInfoTitle()))
            wrapper.like("info_title", infoDTO.getInfoTitle());
        if (infoDTO.getInfoTypeId()!=null)
            wrapper.eq("info_type_id", infoDTO.getInfoTypeId());
        wrapper.eq("deleted", 0)
                .orderByDesc("create_time");
        IPage<InfoVO> infoVOIPage = baseMapper.pageQueryInfo(new Page<>(infoDTO.getPage(),infoDTO.getPageSize()),wrapper);
        log.info("infoVOIPage.getRecords()-->{}",infoVOIPage.getRecords());
        log.info("infoVOIPage.getTotal()-->{}",infoVOIPage.getTotal());
        return Result.success(infoVOIPage.getRecords(),infoVOIPage.getTotal());
    }

    @Override
    public Result queryInfoType(CategoryDTO categoryDTO) {
        if (categoryDTO.getPage()!=0 && categoryDTO.getPageSize()!=0){
            QueryWrapper<CategoryDTO> wrapper = new QueryWrapper<>();
            if (categoryDTO.getName()!=null && !"".equals(categoryDTO.getName()))
                wrapper.like("type_name",categoryDTO.getName());
            IPage<CategoryVO> categoryVOIPage = baseMapper.pageQueryInfoType(new Page<>(categoryDTO.getPage(),categoryDTO.getPageSize()),wrapper);
            return Result.success(categoryVOIPage.getRecords(),categoryVOIPage.getTotal());
        }else {
            List<CategoryVO> categoryVOList = baseMapper.queryInfoType();
            return Result.success(categoryVOList);
        }

    }

    @Override
    public Result updateInfoStatus(InfoDTO infoDTO) {
        UpdateWrapper<Info> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status", infoDTO.getStatus())
                .eq("id",infoDTO.getId());
        if (infoDTO.getStatus()!=0){
            updateWrapper.set("publish_time", LocalDateTime.now());
        }else {
            updateWrapper.set("publish_time", null);
        }
        int i = baseMapper.update(new Info(), updateWrapper);
        if (i>0)
            return Result.success();
        throw new OperationDataException(ResultCode.OPERATION_DATA_ERROR);
    }

    @Override
    public Result logicDeleteInfoById(Integer id) {
        int i = baseMapper.deleteById(id);
        if (i>0)
            return Result.success();
        throw new OperationDataException(ResultCode.OPERATION_DATA_ERROR);
    }

    @Override
    public Result logicBatchDeleteInfoByIds(List<Integer> ids) {
        int i = baseMapper.deleteBatchIds(ids);
        if (i>0)
            return Result.success();
        throw new OperationDataException(ResultCode.OPERATION_DATA_ERROR);
    }

    @Override
    public Result queryInfoById(Integer id) {
        Info info = baseMapper.selectById(id);
        if (info == null)
            throw new DataLoadingException(ResultCode.DATA_LOADING_ERROR);
        return Result.success("请求成功",info);
    }

    @Override
    public Result updateInfo(InfoDTO infoDTO) {
        Info info = new Info();
        BeanUtils.copyProperties(infoDTO,info);
        int i = baseMapper.updateById(info);
        if (i<=0)
            throw new OperationDataException(ResultCode.OPERATION_DATA_ERROR);
        return Result.success();
    }

    @Override
    public Result addInfo(Integer num) {
        ArrayList<Info> infoList = new ArrayList<>();
        String baseUrl = "http://www.cnsf99.com";
        String element = HttpClientUtil.getElement(baseUrl+"/News/index.html?page=1&id=454");
        List<String> urlList = JsoupUtil.parseUrlList(baseUrl, element);

        int count = 0;
        for (String url : urlList) {
            count+=1;
            if (count>num)
                break;

            String element1 = HttpClientUtil.getElement(url);
            Info info = JsoupUtil.getInfo(element1);
            info.setCreateTime( new Date());
            info.setUpdateTime(new Date());
            infoList.add(info);
        }
        log.info("infoList-->{}",infoList);
        int i = baseMapper.insertBatch(infoList);
        if (i<=0)
            throw new OperationDataException(ResultCode.DATA_LOADING_ERROR);
        return Result.success();
    }


    @Override
    public Result getHomeInfo() {
        QueryWrapper<Info> wrapper = new QueryWrapper<>();
        wrapper.groupBy("it.type_name");
        List<HomeDataVO> homeInfoList = baseMapper.getHomeInfoData(wrapper);
        return Result.success(homeInfoList);
    }
}
